﻿#include "GUILoadHVACNoiseDialog.h"
#include "ui_GUILoadHVACNoiseDialog.h"
#include "FITK_Component/FITKAcousticsModel/FITKAbstractSEASubSys.h"

#include "FITK_Component/FITKAcousticsPhysics/FITKLoadNoiseHVAC.h"

#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppSettings.h" 
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"

#include <QMessageBox>
#include <QFileDialog>
#include <QFileInfo>

namespace GUI {
    GUILoadHVACNoiseDialog::GUILoadHVACNoiseDialog(Core::FITKActionOperator* oper, QWidget* parent /*= nullptr*/) :
        GUILoadBaseDialog(nullptr, oper, parent),
        _ui(new Ui::GUILoadHVACNoiseDialog)
    {
        _ui->setupUi(this);
        //创建新载荷数据
        this->initWidgetImage();
        this->createNewLoadHVACNoiseObjData();
        this->init();
    }

    GUILoadHVACNoiseDialog::GUILoadHVACNoiseDialog(Acoustics::FITKAcousticsAbstractLoad* obj, Core::FITKActionOperator* oper, QWidget* parent /*= nullptr*/) :
        GUILoadBaseDialog(obj, oper, parent),
        _ui(new Ui::GUILoadHVACNoiseDialog)
    {
        _ui->setupUi(this);
        //初始化 
        this->initWidgetImage();
        this->init();
    }

    GUILoadHVACNoiseDialog::~GUILoadHVACNoiseDialog()
    {
        //释放数据
        if (_ui) {
            delete _ui;
            _ui = nullptr;
        }
    }

    void GUILoadHVACNoiseDialog::init()
    {
        if (!_obj) return;
        //初始化数据
        QString name = _obj->getDataObjectName();
        if (name.isEmpty())
        {
            name = "HVACNoise-1";
            name = this->ckeckNameFromMgr(name);
        }
        QString connectionName = _obj->getConnectionObjName();
        bool isEnabledState = _obj->getEnabledState();
        _ui->lineEdit_Name->setText(name);
        _ui->lineEdit_Connection->setText(connectionName);
        _ui->checkBox_EnabledState->setCheckState((isEnabledState ? Qt::CheckState::Checked : Qt::CheckState::Unchecked));
        //设置路径
        Acoustics::FITKLoadAbsImport* importData = dynamic_cast<Acoustics::FITKLoadAbsImport*>(_obj);
        if (!importData) return;
        _ui->lineEdit_Spectra->setText(importData->getFilePath());
    }

    void GUILoadHVACNoiseDialog::on_pushButton_Cancel_clicked()
    {
        //点击取消按键动作
        this->reject();
    }

    void GUILoadHVACNoiseDialog::on_pushButton_OK_clicked()
    {
        if (!_oper)return;
        QString filePath = _ui->lineEdit_Spectra->text();
        if (filePath.isEmpty() || !QFileInfo(filePath).isFile())
        {
            QMessageBox::warning(this, "", tr("The HVAC-Noise Spectra File not exist !!!"), QMessageBox::Ok);
            return;
        }


        //获取操作器动作类型
        QString operType;
        _oper->argValue<QString>("OperModelType", operType);
        if (operType.isEmpty())
        {
            delete _obj;
            this->reject();
            return;
        }
        //创建动作
        if (operType == "Create")
        {
            if (!this->createObjData())
                return;
        }
        //修改动作
        else if (operType == "Edit")
        {
            if (!this->editObjData())
                return;
        }
        this->accept();
    }

    void GUILoadHVACNoiseDialog::on_pushButton_Spectra_clicked()
    {
        //主界面
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        //获取工作目录
        QString wk;
        auto settings = FITKAPP->getAppSettings();
        if (settings)
            wk = settings->getWorkingDir();
        QFileDialog dlg(mw, QObject::tr("Open HVACNoise File"), wk, "CSV(*.csv)");
        dlg.setAcceptMode(QFileDialog::AcceptOpen);  //打开模式
        dlg.show();
        //阻塞，否则键盘事件处理会出问题
        bool accept = false;
        QEventLoop loop;
        connect(&dlg, &QFileDialog::accepted, [&] {loop.quit(); accept = true;  });
        connect(&dlg, &QFileDialog::rejected, [&] {loop.quit(); accept = false;  });
        loop.exec();

        if (!accept) return;
        //选择的文件名称
        QStringList files = dlg.selectedFiles(); 
        if (files.isEmpty()) return;
        QString fileName = files.at(0);
        _ui->lineEdit_Spectra->setText(fileName);
    }

    void GUILoadHVACNoiseDialog::createNewLoadHVACNoiseObjData()
    {
        //获取拾取数据
        if (!_oper) return;
        int pickObjID = 0;
        if (!_oper->argValue<int>("PickObjID", pickObjID)) return;
        double connectionLocation[3]{ 0.0,0.0,0.0 };
        if (!_oper->argValue<double>("connectPointX", connectionLocation[0])) return;
        if (!_oper->argValue<double>("connectPointY", connectionLocation[1])) return;
        if (!_oper->argValue<double>("connectPointZ", connectionLocation[2])) return;
        //获取拾取类型
        bool isSeaModel = false;
        int seaType = 0;
        if (!this->getPickDiscDataType(pickObjID, isSeaModel, seaType)) return;
        //创建新的数据
        if (isSeaModel == false) return;
        if (seaType == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEACavity)
            _obj = new Acoustics::FITKLoadNoiseHVAC;
        if (!_obj) return;
        //设置连接器对象
        _obj->setConnectionObjID(pickObjID);
        _obj->setConnectionLocation(connectionLocation);
    }

    void GUILoadHVACNoiseDialog::initWidgetImage()
    {
        //初始化图像
        _ui->label->setPixmap(QPixmap(":/ToolBar/toolBar/Import_HVAC_Noise.png"));
    }

    void GUILoadHVACNoiseDialog::getDataFormWidget()
    {
        Acoustics::FITKLoadAbsImport* importData = dynamic_cast<Acoustics::FITKLoadAbsImport*>(_obj);
        if (!importData) return;
        //从窗口获取数据
        bool isEnabledState = _ui->checkBox_EnabledState->checkState() == Qt::CheckState::Checked;
        QString filePath = _ui->lineEdit_Spectra->text();
        //设置数据
        importData->setEnabledState(isEnabledState);
        importData->setFilePath(filePath);
    }

    QString GUILoadHVACNoiseDialog::getNameFormWidget()
    {
        return _ui->lineEdit_Name->text();
    }
}
